using System;
using UnityEditor;
using UnityEngine;

namespace ZhenG.Toolbar
{
    public class ToolbarExtend
    {
        private const float PopupWidth = 120f;
        private GUIStyle _popupStyle;
        private GUIStyle _iconButtonStyle;

        public ToolbarExtend()
        {
            _popupStyle = new GUIStyle(EditorStyles.toolbarPopup) { alignment = TextAnchor.MiddleCenter, fixedWidth = PopupWidth };
            _iconButtonStyle = new GUIStyle(EditorStyles.toolbarButton) { fixedWidth = 30, fixedHeight = 20, padding = new RectOffset(2, 2, 2, 2) };
        }

        public void DrawRightToolbar()
        {
            
        }

        public void DrawLeftToolbar()
        {
            GUILayout.Space(3);
            OnRefreshButton();
            GUILayout.Space(3);
            OnTimeScale();
            GUILayout.Space(3);
            OnQualitySettings();
        }

        #region 时间缩放

        private int _timeScaleIndex = 1;
        private readonly string[] _timeScaleOptions = { " 0.5x", " 1x", " 1.5x", " 2x", " 3x" };

        private void OnTimeScale()
        {
            _timeScaleIndex = EditorGUILayout.Popup(_timeScaleIndex, _timeScaleOptions, _popupStyle, GUILayout.Width(PopupWidth));
            Time.timeScale = ParseTimeScale(_timeScaleOptions[_timeScaleIndex]);
        }

        private float ParseTimeScale(string timeScale)
        {
            timeScale = timeScale.Replace("x", String.Empty);
            return float.Parse(timeScale);
        }

        #endregion

        #region 质量设置

        private void OnQualitySettings()
        {
            int currentQuality = EditorGUILayout.Popup(QualitySettings.GetQualityLevel(), QualitySettings.names, _popupStyle, GUILayout.Width(80));
            QualitySettings.SetQualityLevel(currentQuality);
        }

        #endregion

        #region 刷新按钮

        private void OnRefreshButton()
        {
            // 使用Unity内置的刷新图标
            GUIContent refreshIcon = EditorGUIUtility.IconContent("Refresh");
            refreshIcon.tooltip = "刷新编辑器";

            if (GUILayout.Button(refreshIcon, _iconButtonStyle))
            {
                // 执行刷新操作
                AssetDatabase.Refresh();
            }
        }

        #endregion
    }
}